package com.ftdigital.primemovers_operators.adpter;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.fragment.HistoryFragment;
import com.ftdigital.primemovers_operators.fragment.TradingDetailFragment;
import com.ftdigital.primemovers_operators.fragment.TradingDetailFragment_;
import com.ftdigital.primemovers_operators.fragment.TradingEvaluationFragment;
import com.ftdigital.primemovers_operators.fragment.TradingEvaluationFragment_;
import com.ftdigital.primemovers_operators.model.AddOilHistory;
import com.ftdigital.primemovers_operators.model.FinishlistEntity;
import com.ftdigital.primemovers_operators.model.FinishlistEntityList;

import org.androidannotations.annotations.rest.Post;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import javax.crypto.Cipher;

/**
 * Created by Curry on 2015-12-16.
 */
public class ExpendListviewAdapter implements ExpandableListAdapter {

    private HistoryFragment context;
    //    List<FinishlistEntity> finishlistEntityList = new ArrayList<>();
    private AddOilHistory addOilHistory;
    public ExpendListviewAdapter(HistoryFragment context,  AddOilHistory addOilHistory) {
        this.context = context;
        this.addOilHistory = addOilHistory;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public int getGroupCount() {
        return addOilHistory.getData().size();
    }

    @Override
    public int getChildrenCount(int i) {
        return addOilHistory
                .getData()
                .get(i)
                .getList()
                .size();
    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int position, boolean b, View view, ViewGroup viewGroup) {
//        View groupView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.history_title_item, null);
        GroupViewHolder groupViewHolder;
        if (view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.history_title_item, null);
            groupViewHolder = new GroupViewHolder(view);
            view.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        groupViewHolder.time.setText(addOilHistory.getData().get(position).getTime());
        groupViewHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
//                Toast.makeText(context.getActivity(), "onclick:"+position, Toast.LENGTH_SHORT).show();
                context.expendListviewClick(position);
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context.getActivity()).inflate(R.layout.history_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (addOilHistory.getData().get(groupPosition).getList().get(childPosition).getIs_evaluate().equals("false")) {
            holder.evaluation.setTextColor(context.getResources().getColor(R.color.white));
            holder.evaluation.setText(context.getResources().getString(R.string.evaluation_hint));
            holder.evaluation.setBackgroundColor(context.getResources().getColor(R.color.themeColor));
            holder.evaluation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MainActivity.getInstance().switchFragment(context, TradingEvaluationFragment_.builder()
                            .listEntity(addOilHistory.getData().get(groupPosition).getList().get(childPosition))
                            .build());
                }
            });
        }else{
            holder.evaluation.setTextColor(context.getResources().getColor(R.color.schedule_cancle));
            holder.evaluation.setText(context.getResources().getString(R.string.evaluationed_hint));
            holder.evaluation.setBackgroundColor(context.getResources().getColor(R.color.transparent));
        }

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                MainActivity.getInstance().switchFragment(context, TradingDetailFragment_.builder()
//                        .order_id(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getOrder_id())
//                        .build());
                MainActivity.getInstance().getSupportFragmentManager()
                        .beginTransaction()
                        .hide(context).replace(R.id.content_none_bottom, TradingDetailFragment_.builder()
                        .order_id(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getOrder_id())
                        .build()).addToBackStack(GlobalData.HISTORY).commit();
            }
        });
        holder.company.setText(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getCompany_name());
        holder.company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context.getActivity(), addOilHistory.getData().get(groupPosition).getList().get(childPosition).getCompany_name()+"",Toast.LENGTH_SHORT).show();
            }
        });
        holder.trunkNo.setText(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getCar_num());
        holder.iolNum.setText(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getType());
        holder.dealTime.setText(addOilHistory.getData().get(groupPosition).getList().get(childPosition).getTime());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int i) {

    }

    @Override
    public void onGroupCollapsed(int i) {

    }

    @Override
    public long getCombinedChildId(long l, long l1) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long l) {
        return 0;
    }

    public static class ViewHolder {
        public LinearLayout layout;
        public TextView company;
        public TextView trunkNo;
        public TextView iolNum;
        public TextView dealTime;
        public TextView evaluation;
        public ViewHolder(View v) {
            company = (TextView) v.findViewById(R.id.company);
            trunkNo = (TextView) v.findViewById(R.id.trunk_no);
            iolNum = (TextView) v.findViewById(R.id.iol_num);
            dealTime = (TextView) v.findViewById(R.id.deal_time);
            evaluation = (TextView) v.findViewById(R.id.evaluation);
            layout = (LinearLayout) v.findViewById(R.id.layout);
        }

    }

    public static class GroupViewHolder {
        public TextView time;
        private LinearLayout layout;
        public GroupViewHolder(View v) {
            time = (TextView) v.findViewById(R.id.time);
            layout = (LinearLayout) v.findViewById(R.id.group_layout);
        }

    }
}
